﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CDB.LinqModel.Data;
using CDB.LinqData;
using CDB.LinqModel;
public partial class back_Testimonials : System.Web.UI.Page
{
    #region Variable Declaration
    IDataRepository _dataObject = new DataRepository();
    Testimonial testimonial;
    List<Testimonial> listTestimonials = new List<Testimonial>();
    #endregion Menu
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindTestimonialsGrid();
        }
    }

    private void bindTestimonialsGrid()
    {
        Testimonial testimonial = new Testimonial(_dataObject);
        listTestimonials = testimonial.GetAllTestimonials();

        if (listTestimonials.Count > 0)
        {
            grdTestimonial.UseAccessibleHeader = true;
            grdTestimonial.Visible = true;
            grdTestimonial.DataSource = listTestimonials;
            grdTestimonial.DataBind();
            //grdTestimonial.HeaderRow.TableSection = TableRowSection.TableHeader;
        }
        else
        {
            grdTestimonial.Visible = false;
            divRed.InnerHtml = "<strong>Oh snap!</strong>&nbsp;No Records Found!";
            divRed.Attributes.Add("class", "alert alert-error");
        }
    }
    protected void grdTestimonial_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
    {
        if (e.Row.RowType == System.Web.UI.WebControls.DataControlRowType.DataRow)
        {
            System.Web.UI.WebControls.Label hdnTestimonialId = (System.Web.UI.WebControls.Label)e.Row.FindControl("hdnTestimonialId");
            System.Web.UI.WebControls.Button btnEdit = (System.Web.UI.WebControls.Button)e.Row.FindControl("btnEdit");
            btnEdit.PostBackUrl = "AddTestimonials.aspx?id=" + hdnTestimonialId.Text;
        }
    }
    protected void grdTestimonial_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
    {
        if (e.CommandName.ToString().ToLower() == "editnews")
        {
            System.Web.UI.WebControls.GridViewRow row = (System.Web.UI.WebControls.GridViewRow)(((System.Web.UI.WebControls.Button)e.CommandSource).NamingContainer);
            System.Web.UI.WebControls.Button btnEdit = (System.Web.UI.WebControls.Button)row.FindControl("btnEdit");
            Response.Redirect(btnEdit.PostBackUrl);
        }

        if (e.CommandName.ToString().ToLower().Trim().Equals("deletenews"))
        {
            System.Web.UI.WebControls.GridViewRow row = (System.Web.UI.WebControls.GridViewRow)(((System.Web.UI.WebControls.Button)e.CommandSource).NamingContainer);
            System.Web.UI.WebControls.Label hdnTestimonialId = (System.Web.UI.WebControls.Label)row.FindControl("hdnTestimonialId");
            
            testimonial = new Testimonial(_dataObject);
            testimonial.Id = Convert.ToInt32(hdnTestimonialId.Text);
            testimonial.Delete();

            bindTestimonialsGrid();
            
            divRed.InnerHtml = "<strong>Deletion!</strong>&nbsp;Record Deleted Successfully!";
            divRed.Attributes.Add("class", "alert alert-error");
        }
    }
    protected void grdTestimonial_PageIndexChanging(object sender, System.Web.UI.WebControls.GridViewPageEventArgs e)
    {
        grdTestimonial.PageIndex = e.NewPageIndex;
        bindTestimonialsGrid();
    }
    protected void grdTestimonial_RowCreated(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex == 0)
        {
            e.Row.RowType = DataControlRowType.Header;
        }
        else
        {
            e.Row.RowType = DataControlRowType.DataRow;
        }
    }
}